//
//  FirstVC.m
//  Im
//
//  Created by ExcuseMe on 16/12/19.
//  Copyright © 2016年 1512. All rights reserved.
//

#import "FirstVC.h"
#import "LrcParser.h"
@interface FirstVC ()
@property (weak, nonatomic) IBOutlet UITableView *lrcTable;
@end

@implementation FirstVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.lrcTable.delegate=self;
    self.lrcTable.dataSource=self;
    
  
  UIImageView *iv = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
 [iv setImage:[UIImage imageNamed:@"7.jpg"]];
 [self.view addSubview:iv];
  self.playBtn = [[UIButton alloc]initWithFrame:CGRectMake(135, 500, 50, 50)];
 [self.playBtn setBackgroundImage:[UIImage imageNamed:@"Player"] forState:UIControlStateNormal];
 [self.playBtn addTarget:self action:@selector(playMusic) forControlEvents:UIControlEventTouchUpInside];
 [self.playBtn setTitle:nil forState:UIControlStateNormal];
    //把按钮加到视图
 [self.view addSubview:self.playBtn];
  self.nextM = [[UIButton alloc]initWithFrame:CGRectMake(205, 505,40 ,40)];
 [self.nextM setBackgroundImage:[UIImage imageNamed:@"NextA"] forState:UIControlStateNormal];
 [self.view addSubview:self.nextM];
  self.lastM = [[UIButton alloc]initWithFrame:CGRectMake(75, 505, 40 ,40)];
 [self.lastM setBackgroundImage:[UIImage imageNamed:@"NextB"] forState:UIControlStateNormal];
 [self.view addSubview:self.lastM];
    
    //为按钮添加响应机制
  NSString *string = [[NSBundle mainBundle]pathForResource:@"偶阵雨" ofType:@"mp3"];
  NSURL *url = [NSURL fileURLWithPath:string];
  avAudioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
  avAudioPlayer.delegate = self;
  avAudioPlayer.volume = 1;
    //设置音乐播放次数，－1表示一只循环
    avAudioPlayer.numberOfLoops = -1;
    [avAudioPlayer prepareToPlay];
    progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(50, 470, 200, 20)];
    [self.view addSubview:progressView];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
                                                                   selector:@selector(playProgress)
                                                                  userInfo:nil repeats:YES];
    volSlider = [[UISlider alloc]initWithFrame:CGRectMake(50, 70, 200, 20)];
    [volSlider addTarget:self action:@selector(volumeChange)
                             forControlEvents:UIControlEventValueChanged];
    volSlider.minimumValue = 0.0f;
    volSlider.maximumValue = 10.0f;
    volSlider.value = 5.0f;
    [self.view addSubview:volSlider];
    UISwitch *swi = [[UISwitch alloc]initWithFrame:CGRectMake(250, 20, 60, 40)];
   [swi addTarget:self action:@selector(onOrOff:) forControlEvents:UIControlEventValueChanged];
    //默认状态为打开
    swi.on =YES;
    [self.view addSubview:swi];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    

    
    
}
-(void)playMusic{
    if(!_isPlay){
        //播放
        [avAudioPlayer play];
        //修改按钮的图片
        [self.playBtn setBackgroundImage:[UIImage imageNamed:@"Pause"] forState:UIControlStateNormal];
        _isPlay=YES;
    }
    else{
        //暂停
        [avAudioPlayer pause];
        //修改按钮的图片
        [self.playBtn setBackgroundImage:[UIImage imageNamed:@"Player"] forState:UIControlStateNormal];
        _isPlay=NO;
    }
}

/*-(void)play
{
    [avAudioPlayer play];
}
-(void)pause
{
    [avAudioPlayer pause];
}*/
/*-(void)stop
{
    avAudioPlayer.currentTime = 0;//当前播放时间设置为0
    [avAudioPlayer stop];
}*/
-(void)playProgress
{
    progressView.progress = avAudioPlayer.currentTime/avAudioPlayer.duration;
}
-(void)onOrOff:(UISwitch *)sender
{
    avAudioPlayer.volume = sender.on;
}
//播放音量的控制
-(void)volumeChange
{
    avAudioPlayer.volume=volSlider.value;
}
 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    [timer invalidate];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
